{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "卷积窗口函数\n",
    "rolling(window=n, min_periods=None, center=False)\n",
    "\n",
    "\n",
    "参数名称|说明\n",
    ":-|:-\n",
    "window|默认值为 1，表示窗口的大小，也就是观测值的数量，\n",
    "min_periods|表示窗口的最小观察值，默认与 window 的参数值相等。\n",
    "center|是否把中间值做为窗口标准，默认值为 False。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             A   B   C   D   E\n",
      "2021-12-17  42  31  27  25  31\n",
      "2021-12-18  81  17  40  74  95\n",
      "2021-12-19  16  71  68  57  63\n",
      "2021-12-20  72  41  70  97  33\n",
      "2021-12-21  59  29  48  51  95\n",
      "2021-12-22  71  63  69  74  84\n",
      "2021-12-23  31  75  92  79   4\n",
      "2021-12-24   7  71  84  54  30\n",
      "2021-12-25  69   5  57  15   0\n",
      "2021-12-26   5  47   3  78  91\n"
     ]
    }
   ],
   "source": [
    "df = pd.DataFrame({\n",
    "    \"A\": pd.Series(np.random.rand(10)*100,index=pd.date_range(\"2021-12-17\",periods=10),dtype=np.int8),\n",
    "    \"B\": pd.Series(np.random.rand(10)*100,index=pd.date_range(\"2021-12-17\",periods=10),dtype=np.int8),\n",
    "    \"C\": pd.Series(np.random.rand(10)*100,index=pd.date_range(\"2021-12-17\",periods=10),dtype=np.int8),\n",
    "    \"D\": pd.Series(np.random.rand(10)*100,index=pd.date_range(\"2021-12-17\",periods=10),dtype=np.int8),\n",
    "    \"E\": pd.Series(np.random.rand(10)*100,index=pd.date_range(\"2021-12-17\",periods=10),dtype=np.int8)\n",
    "})\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             A     B     C     D     E\n",
      "2021-12-17 NaN  36.5  29.0  26.0  28.0\n",
      "2021-12-18 NaN  49.0  28.5  57.0  84.5\n",
      "2021-12-19 NaN  43.5  69.5  62.5  60.0\n",
      "2021-12-20 NaN  56.5  55.5  83.5  65.0\n",
      "2021-12-21 NaN  44.0  38.5  49.5  73.0\n",
      "2021-12-22 NaN  67.0  66.0  71.5  79.0\n",
      "2021-12-23 NaN  53.0  83.5  85.5  41.5\n",
      "2021-12-24 NaN  39.0  77.5  69.0  42.0\n",
      "2021-12-25 NaN  37.0  31.0  36.0   7.5\n",
      "2021-12-26 NaN  26.0  25.0  40.5  84.5\n"
     ]
    }
   ],
   "source": [
    "# 窗口卷积函数\n",
    "print(df.rolling(window=2,axis=1).mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                    A          B          C          D          E\n",
      "2021-12-17        NaN        NaN        NaN        NaN        NaN\n",
      "2021-12-18        NaN        NaN        NaN        NaN        NaN\n",
      "2021-12-19  46.333333  39.666667  45.000000  52.000000  63.000000\n",
      "2021-12-20  52.750000  40.000000  51.250000  63.250000  55.500000\n",
      "2021-12-21  54.000000  37.800000  50.600000  60.800000  63.400000\n",
      "2021-12-22  56.833333  42.000000  53.666667  63.000000  66.833333\n",
      "2021-12-23  53.142857  46.714286  59.142857  65.285714  57.857143\n",
      "2021-12-24  47.375000  49.750000  62.250000  63.875000  54.375000\n",
      "2021-12-25  49.777778  44.777778  61.666667  58.444444  48.333333\n",
      "2021-12-26  45.300000  45.000000  55.800000  60.400000  52.600000\n"
     ]
    }
   ],
   "source": [
    "# 拓展窗口函数\n",
    "print(df.expanding(min_periods=3).mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                    A          B          C          D          E\n",
      "2021-12-17  42.000000  31.000000  27.000000  25.000000  31.000000\n",
      "2021-12-18  71.250000  20.500000  36.750000  61.750000  79.000000\n",
      "2021-12-19  33.000000  55.461538  58.384615  58.461538  67.923077\n",
      "2021-12-20  59.325000  45.700000  66.225000  84.475000  44.350000\n",
      "2021-12-21  59.107438  34.520661  54.024793  62.066116  78.256198\n",
      "2021-12-22  67.046703  53.532967  64.021978  70.032967  82.090659\n",
      "2021-12-23  43.004575  67.850869  82.682525  76.013724  30.006404\n",
      "2021-12-24  18.997866  69.950610  83.560976  61.335671  30.002134\n",
      "2021-12-25  52.334316  26.648003  65.852759  30.443654   9.999695\n",
      "2021-12-26  20.777571  40.216231  23.950210  62.148422  64.000813\n"
     ]
    }
   ],
   "source": [
    "# ewm() 指数加权运算\n",
    "print(df.ewm(com=0.5).mean())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
